package com.core.life.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.core.common.dao.BaseDaoImpl;
import com.core.life.dao.IAccountingRelationsDao;
import com.core.life.entity.AccountingRelationsEntity;

@Repository
public class AccountingRelationsDaoImpl extends BaseDaoImpl<AccountingRelationsEntity>
implements IAccountingRelationsDao{

	@Override
	public void deleteByConsumeCountIds(List<Long> consumeRecordIds) {
		String hql = "delete from AccountingRelationsEntity ar where ar.consumeRecordId in(:consumeRecordIds)";
		this.getSession().createQuery(hql).setParameterList("consumeRecordIds", consumeRecordIds).executeUpdate();
	}

	@Override
	public Double selectSumPayMoney(Integer year,Integer month, Long sourceUserId, Long targetUserId) {
		String hql = "select sum(ar.payMoney) from AccountingRelationsEntity ar"+
					 " where ar.year=:year and ar.month=:month"+ 
					 " and ar.sourceUserId=:sourceUserId and ar.targetUserId=:targetUserId";
		Query sumQuery = this.getSession().createQuery(hql);
		Object obj = sumQuery.setParameter("year", year)
				.setParameter("month", month)
				.setParameter("sourceUserId", sourceUserId)
				.setParameter("targetUserId", targetUserId).uniqueResult();
		double sum = 0;
		if(null != obj)
			sum = (double) obj; 
		return sum;
	}

    @SuppressWarnings("unchecked")
    @Override
    public List<AccountingRelationsEntity> getByConsumeRecordId(Long consumeRecordId) {
        String hql = "from AccountingRelationsEntity ar where ar.consumeRecordId =:consumeRecordId";
        return this.getSession().createQuery(hql).setParameter("consumeRecordId", consumeRecordId).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Object[]> selectSumByConsumeRecordIds(List<Long> consumeRecordIds) {
        String sql = "SELECT t.sourceUserId,t.targetUserId, sum(t.payMoney) from t_accounting_relations t where t.consumeRecordId in (:consumeRecordIds) GROUP BY t.sourceUserId,t.targetUserId ;";
        return this.getSession().createSQLQuery(sql).setParameterList("consumeRecordIds", consumeRecordIds).list();
    }

}
